var restify = require('restify');
var client = restify.createJsonClient({
	url: 'http://localhost:8060'
});

var users = require('../users.json')


function testRegister (req, res, next){
	
//	"cus_phone": req.body.cus_phone,
//	"cus_pwd": req.body.cus_pwd,
//	"sex": req.body.sex,
//	"nickname":req.body.nickname
	var user = new Object();
	user = users["P1"];
	client.post("/register",user, function(err, req0, res0, data){
		if(err){
			console.log(err);
		}else{
			res.send("test result:\n" + JSON.stringify(data));
		}
	});
	return next();
}

function testLogin(req, res, next){
	var user = new Object();
	user = users["P1"];
	var loginUser = {"kind":"c","cus_phone":user.cus_phone,"cus_pwd":user.cus_pwd};
	console.log(loginUser);
	client.post("/cuslogin",loginUser, function(err, req0, res0, data){
		
		if(err){
			console.log(err);
		}else{
			res.send("test result:\n" + JSON.stringify(data));
		}
	});
	return next();
}

function testUpdate(req, res, next){
	var user = new Object();
	user.kind = 'c';
	user.cus_id = '520da03e4036edec19000001';
	user.cus_phone = '222111';
	user.nickname = 'bim';
	user.add1 = 'bim';
	user.add4 = 'bm';
	client.post("/update", user, function(err, req0, res0, data){
		
		if(err){
			console.log(err);
		}else{
			res.send("test result:\n" + JSON.stringify(data));
		}
	});
	return next();
}

function testCheckPhone(req, res, next){
	var user = new Object();
	user.kind = 'c';
	user.cus_phone = "222111";
	client.post("/checkPhone", user, function(err, req0, res0, data){
		if(err){
			console.log();
		}else {
			res.send("test result:\n" + JSON.stringify(data));
		}
	});
}

function testGetInfo(req, res, next){
	var user = new Object();
	user.kind = 'c';
	user.cus_id = '520da03e4036edec19000001';
	client.post("/personalinfo", user, function(err, req0, res0, data){
		
		if(err){
			console.log(err);
		}else{
			res.send("test result:\n" + JSON.stringify(data));
		}
	});
	return next;
}
function logOffTest(req, res, next) {
	console.log("logoff");
	var result = new Object();
	var update = new Object();
	for(var key in req.query){
		update[key] = req.query[key];
	}
	console.log(update);
//	result.status= status.SUCCESS.VALUE + 'ad';
//	var locations = new Object();
//	locations.destination = "31.202928,121.557706";
//	locations.origin = "31.213719,121.537192";
//	require("../private/googleService/service").getDistance(locations, function(err, data){
//		if (!err) {
//		     console.log("in");
//			 result.distance = {
//				'status' : 0,
//				'distance' : data.routes[0].legs[0].distance,
//				'duration' : data.routes[0].legs[0].duration
//			};
//			
//		} else {
//			var result = {
//				'status' : -1
//			};
//			res.send(result);
//		}
//	});
//	function sleep(milliSecond) {  	      
//	    var startTime = new Date().getTime();  	      
//	    console.log(startTime);        
//	    while(new Date().getTime() <= milliSecond + startTime) {  	          
//	    }      
//	    console.log(new Date().getTime());  
//	}  
//	//sleep(20000);
//	console.log("out");
//	res.send(result);
	var MongoClient = require('mongodb').MongoClient;
	var ObjectID = require('mongodb').ObjectID;
//	MongoClient.connect(dataConfig.host + dataConfig.db, function(err, db) {
//
//			//console.log(doc._id === new ObjectID('520da03e4036edec19000001'));
//			db.collection("customer").findOne({_id:new ObjectID('520da03e4036edec19000001')},function(err,doc1){
//				console.log(doc1);
//			}
//			);
//	});

	MongoClient.connect(dataConfig.host + dataConfig.db, function(err, db) {
		if (!err) {
			console.log('db is connected!');
			this._collection = db.collection("customer");

			this._collection.update({_id:new ObjectID('520da03e4036edec19000001')}, {$set:{cus_pwd:'aa'}},function(err, result){	
				if(!err){
				console.log(result);	
				}else{
					console.log(err);
				}
			});
		} else {
			console.log(err);
		}
	});
//	console.log(typeof ((req.query.id - 1) * 10));
//	var query = new Object();
//	query.$or = [{phone:"2134"},{phone:"123"}];
//	console.log(query);
//	db.find(dataConfig.col_customer, query, function(err1, doc){
//		if(err1){
//			result.status = -1;
//			res.send(result);
//		}else{
//			if (doc === null) {
//				result.status = 1;
//				result.doc = doc;
//				result.doc1 = "123";
//				console.log(result);
//				res.send(result);
//			} else {
//				result.status = 0;
//				result.doc = doc;
//				console.log(result);
//				res.send(result);
//			}
//		}
//	});
//	console.log(req.query);
//	var s = req.query.id;
//	var j = eval('(' + s + ')');
//	for(var ss in s){
//		console.log(s[ss]);
//	}
//	for(var s in status){
//		console.log(s);
//	}
//	console.log(req.query.id);
//	if(req.query.id[0] instanceof Number){
//		
//	}else{
//		console.log(req.query.id[0] instanceof Number);
//	}
//	console.log(typeof req.query.id[0]);
//	console.log(req.query.id[0] instanceof String);
//	var t = {"s":1};
//	console.log(t);
//	console.log(typeof t);
//	console.log(typeof t.s);
//	console.log(JSON.stringify(t));
//	console.log(typeof JSON.stringify(t));
	return next();
};
exports.services = [ testRegister, testLogin, testUpdate, testCheckPhone, testGetInfo ];